package com.taotao.cart.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.taotao.cart.bean.User;
import com.taotao.cart.bean.UserThreadLocal;
import com.taotao.cart.pojo.Cart;
import com.taotao.cart.service.CartCookieService;
import com.taotao.cart.service.CartService;

@RequestMapping("cart")
@Controller
public class CartController {

	@Autowired
	private CartService cartService;
	@Autowired
	private CartCookieService cartCookieService;
	
	/**
	 * cart/add/{itemId}.html
	 * '加入购物车':
	 * 1.用户未登录的情况下添加商品到购物车
	 * 2.用户已登录的情况下添加商品到购物车
	 *  
	 *  同一个请求，有多套处理流程
	 */
	@RequestMapping("add/{id}")
	public String addItemToCart(HttpServletRequest request,
			HttpServletResponse response,@PathVariable("id") Long itemId){
		User user = UserThreadLocal.get();
		try {
			if(user == null){
				//走未登录的加入购物车流程 保存到cookie
				this.cartCookieService.addItemToCart(request,response,itemId);
			}else{
				//走已登录的加入购物车流程  保存到数据库
				this.cartService.addItemToCart(itemId,user.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//重定向到 ‘我的购物车’ 页面
		return "redirect:/cart/list.html";
	}
	
	
	
	/**
	 * cart/list
	 * 查询购物车列表
	 * 跳转到cart.jsp = ‘我的购物车’
	 */
	@RequestMapping("list")
	public ModelAndView myCart(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("cart");
		User user = UserThreadLocal.get();
		List<Cart> cartList = null;
		try {
			if(user == null){
				//走未登录的购物车流程 
				cartList = this.cartCookieService.queryCartListByUserId(request);
			}else{
				//走已登录的购物车流程   查询购物车列表
				cartList = this.cartService.queryCartListByUserId(user.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("cartList", cartList);
		return mv;
	}
	
	
	/**
	 * update/num/{itemId}/{num}
	 * 更新购物车商品数量
	 * 同步还是异步？ 异步
	 * 参数 占位符传参  商品id 商品数量
	 * 结果 Void
	 */
	@RequestMapping(value="update/num/{id}/{num}",method=RequestMethod.POST)
	public ResponseEntity<Void> updateCartNum(HttpServletRequest request,
			HttpServletResponse response,@PathVariable("id") Long itemId,
			@PathVariable("num") Integer num){
		User user = UserThreadLocal.get();
		try {
			if(user == null){
				//说明未登录，进行未登录处理
				this.cartCookieService.updateCart(request,response,itemId,num);
			}else{
				//说明已登录，进行已登录的处理
				this.cartService.updateCart(itemId,user.getId(),num);
			}
			return ResponseEntity.status(HttpStatus.OK).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	/**
	 * delete/1023438
	 * 删除购物车商品 
	 */
	@RequestMapping("delete/{id}")
	public String deleteItemToCart(HttpServletRequest request,
			HttpServletResponse response,@PathVariable("id") Long itemId){
		User user = UserThreadLocal.get();
		try {
			if(user == null){
				//走未登录的加入购物车流程 保存到cookie
				this.cartCookieService.deleteItemToCart(request,response,itemId);
			}else{
				//走已登录的加入购物车流程  保存到数据库
				this.cartService.deleteItemToCart(itemId,user.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//重定向到 ‘我的购物车’ 页面
		return "redirect:/cart/list.html";
	}
	
	
	/**
	 * 对外提供的接口，根据用户id查询该用户的购物车商品列表
	 */
	@RequestMapping(value="list/{id}",method=RequestMethod.GET)
	public ResponseEntity<List<Cart>> queryCartList(@PathVariable("id") Long userId){
		List<Cart> list = this.cartService.queryCartListByUserId(userId);
		return ResponseEntity.status(HttpStatus.OK).body(list);
	}
	
	
	
	
	
	
}
